require('page/common/header/index.js')
var _account_service = require('service/account-service.js')
var _order_service = require('service/order_service');
const _util = require('../../../util/util')

var newOrderForm = {
    init: function () {
        this.loadInfo();
        this.bindEvents();
    },

    loadInfo: function () {
        _account_service.checkLogin(
            res => {
                //获取了账号信息
                if(res.status===0){
                    $('#firstName').val(res.data.firstName)
                    $('#lastName').val(res.data.lastName)
                    $('#address1').val(res.data.address1)
                    $('#address2').val(res.data.address2)
                    $('#city').val(res.data.city)
                    $('#state').val(res.data.state)
                    $('#zip').val(res.data.zip)
                    $('#country').val(res.data.country)
                } 
            },
            errMsg => {

            }
        )
    },

    bindEvents: function(){
        this.shipClicked();
        this.continueClicked();
    },

    //为shipping的checkbox绑定单击事件
    shipClicked: function(){
        $("#shippingCheckbox").on('click', function () {
            if (this.checked) {
                html = `<tr> <th colspan=2>Shipping Address</th> </tr> 
                <tr> <td>First name:</td> <td><input type='text' name='order.shipToFirstName' value='' id="shipToFirstName"/></td> </tr> 
                <tr> <td>Last name:</td> <td><input type='text' name='order.shipToLastName' value='' id="shipToLastName"/></td> </tr>
                <tr> <td>Address 1:</td> <td><input type='text' size='40' name='order.shipAddress1' value='' id="shipAddress1"/></td> </tr>
                <tr> <td>Address 2:</td> <td><input type='text' size='40' name='order.shipAddress2' value='' id="shipAddress2"/></td> </tr>
                <tr> <td>City:</td> <td><input type='text' name='order.shipCity' value='' id="shipCity"/></td> </tr>
                <tr> <td>State:</td> <td><input type='text' size='4' name='order.shipState' value='' id="shipState"/></td> </tr>
                <tr> <td>Zip:</td> <td><input type='text' size='10' name='order.shipZip' value='' id="shipZip"/></td> </tr> 
                <tr> <td>Country:</td> <td><input type='text' size='15' name='order.shipCountry' value='' id="shipCountry"/></td> </tr>`
                $("#addressTable").append(html)
            } else {
                $("#addressTable").empty();
            }
        });
    },

    //为continue按钮绑定单击事件
    continueClicked: function(){
        $("#newOrder").on('click', function () {
            var order = {};
            order.expiryDate = $("#date").val();
            order.billToFirstName = $("#firstName").val();
            order.creditCard = $("#cardNumber").val();
            order.billToLastName = $("#lastName").val();
            order.billAddress1 = $("#address1").val();
            order.billAddress2 = $("#address2").val();
            order.billCity = $("#city").val();
            order.billState = $("#state").val();
            order.billZip = $("#zip").val();
            order.billCountry = $("#country").val();
            order.cardType = $("#cardType").val();
            order.shippingAddressRequired = $("#shippingCheckbox").prop("checked");
            if (order.shippingAddressRequired) {
                order.shipToFirstName = $("#shipToFirstName").val();
                order.shipToLastName = $("#shipToLastName").val();
                order.shipAddress1 = $("#shipAddress1").val();
                order.shipAddress2 = $("#shipAddress2").val();
                order.shipCity = $("#shipCity").val();
                order.shipState = $("#shipState").val();
                order.shipZip = $("#shipZip").val();
                order.shipCountry = $("#shipCountry").val();
            }
            
            _order_service.newOrder(
                order, 
                res => { 
                    window.location.href = `../order/view-order.html?orderId=${res.data.orderId}`; 
                }, 
                errMsg => {

                }
            )
        })
    }
}

module.exports = newOrderForm.init();